package com.lx.bbs.controller;


import com.lx.bbs.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class UeditorController {


    @RequestMapping(path = "js/ueditor-1.4.3.3/ctrl", params="action=config")
    public Map<String, Object> ueditorConfig(){
        Map<String, Object> ret = new HashMap<>();
        // 执行上传图片方法
        ret.put("imageActionName", "uploadImage");
        //提交表单的控件名
        ret.put("imageFieldName", "file");
        // 设置图片访问路径前缀
        ret.put("imageUrlPrefix", "");
        return ret;
    }

    @Autowired
    UserController userController;

    @RequestMapping(path = "js/ueditor-1.4.3.3/ctrl", params="action=uploadImage")
    public Map<String, Object> uploadImage(@RequestParam("file") MultipartFile file) throws IOException {
        Map<String, Object> ret = new HashMap<>();
        Result result = userController.uploadHead(file);
        ret.put("state", "SUCCESS");
        ret.put("url", result.getData().toString());
        ret.put("original", file.getOriginalFilename());
        ret.put("title", result.getData().toString().substring(result.getData().toString().lastIndexOf("/") + 1));
        return ret;
    }

}
